package com.kh.pajx.sop.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.dao.IHsSchoolRelationDao;
import com.kh.pajx.sop.domain.HsSchoolRelation;
import com.kh.pajx.sop.service.IHsSchoolRelationService;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.base.BaseServiceImpl;

/**
 * 学校扩展信息业务逻辑操作实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月28日 上午9:53:13
 */
@Service
@Transactional
public class HsSchoolRelationServiceImpl extends BaseServiceImpl<HsSchoolRelation> implements IHsSchoolRelationService {

	@Resource
	private IHsSchoolRelationDao hsSchoolRelationDao;
	
	
	@Override
	public HsSchoolRelation getById(String uuid) {
		return hsSchoolRelationDao.getById(uuid);
	};
	
	@Override
	public void save(HsSchoolRelation demo) {
		hsSchoolRelationDao.save(demo);
	}
	
	@Override
	public void update(HsSchoolRelation demo) {
		hsSchoolRelationDao.update(demo);
	}
	
	@Override
	public void delete(String uuid) {
		hsSchoolRelationDao.delete(uuid);
	}
	
	@Override
	public List<HsSchoolRelation> findAll() {
		return hsSchoolRelationDao.findAll();
	}
	
	@Override
	public List<HsSchoolRelation> getRelation(String sclId) {
		return hsSchoolRelationDao.getRelation(sclId);
	}
	
	
	@Override
	public List<HsSchoolRelation> getRelationByFlag(String sclId, String flag) {
		return hsSchoolRelationDao.getRelationByFlag(sclId, flag);
	}
	
	@Override
	public HsSchoolRelation getBySclId(String sclId) {
		List<HsSchoolRelation> relList = hsSchoolRelationDao.getRelationByFlag(sclId, "1");
		if(relList!=null && relList.size()>0){
			return relList.get(0);
		}
		return null;
	}
	
	
	public void bindEcinfo(String sclId,String ecId,String userId){
		// 获取学校扩展信息
		HsSchoolRelation schoolRelation = getBySclId(sclId);
		HsSchoolRelation tempRelation = new HsSchoolRelation();
		BeanUtils.copyProperties(schoolRelation, tempRelation);
		tempRelation.setEcId(ecId);
		tempRelation.setEcIdBandTime(FormatDate.dateTimeDetail());
		tempRelation.setCreateTime(FormatDate.dateTimeDetail());
		tempRelation.setCreateOperId(userId);
		
		// 添加新数据
		hsSchoolRelationDao.save(tempRelation);
		
		// 更新旧数据
		schoolRelation.setRelationStatusFlag("0");
		hsSchoolRelationDao.update(schoolRelation);
		
	}
	
	@Override
	public void removeEcid(String sclId, String userId) throws Exception {
		HsSchoolRelation updateRel = getBySclId(sclId);
		HsSchoolRelation saveRel = new HsSchoolRelation();
		
		BeanUtils.copyProperties(updateRel, saveRel);
		saveRel.setEcId(null);
		saveRel.setEcIdBandTime(null);
		saveRel.setCreateTime(FormatDate.dateTimeDetail());
		saveRel.setCreateOperId(userId);
		
		hsSchoolRelationDao.save(saveRel);
		
		updateRel.setRelationStatusFlag("0");
		hsSchoolRelationDao.update(updateRel);
	}
}
